package com.example.supplychainbackend.entity.design;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Entity
@Table(name = "ecn")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ECNEntity {
    @Id
    String	ecnNumber;

    String	salesOrderNumber;
    String	salesOrderName;
    String	contractNumber;
    String	contractName;
    String	modifier;
    String	modifyTime;
    Integer	status;
    String	parentMaterial;
    String	parentMaterialName;
    String	changeReason;
    String	changeDescription;
    String	effectiveDate;
    String	department;
    String	version;
    String	remark;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "ecn_d")
    List<ECNDetailEntity> bomChangeDetail;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "ecn_r")
    List<ECNRelatedEntity> changeRelated;

    @Transient
    private static final AtomicInteger counter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (ecnNumber == null || ecnNumber.equals("")) {
            ecnNumber = String.format("%s%s%03d",
                    "ECN", DateTimeUtils.getCurrentDateForID(), counter.getAndIncrement());
        }
    }
}
